<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <input type="checkbox" name="" id="1">1
  <input type="checkbox" name="" id="2">2
  <input type="checkbox" name="" id="3">3
  <input type="checkbox" name="" id="4">4
  <input type="checkbox" name="" id="5">5
  <input type="checkbox" name="" id="6">6
  <input type="checkbox" name="" id="7">7
  <input type="checkbox" name="" id="8">8
  <input type="checkbox" name="" id="9">9

  <script>
    var synchronousFile = function(id) {
      console.log('开始同步文件，id为：' + id);
    }

    var proxySynchronousFile = (function() {
      var cache = [],     //保存一段时间内需要同步的ID
          timer;          //定时器

      return function(id) {
        cache.push(id);
        if(timer) {       //保证不会覆盖已经启动的定时器
          return;
        }

        timer = setTimeout(function(){
          synchronousFile(cache.join(','))    //两秒内向本体发送需要同步的ID集合
          clearTimeout(timer);
          timer = null;
          cache.length = 0;
        },2000)
      }
    })()


    var checkbox = document.getElementsByTagName("input");
    for(var i = 0,c;c = checkbox[i++];) {
      c.onclick = function() {
        if(this.checked == true) {
          proxySynchronousFile(this.id);
        }
      }
    };
  </script>
</body>
</html>